package cn.shop.admin.service.impl;


import cn.hutool.core.util.NumberUtil;
import cn.shop.admin.service.*;
import cn.shop.transcation.detail.entity.FundDetail;
import cn.shop.transcation.frontUserModule.FrontUser;
import cn.shop.transcation.merchantUser.mapper.entity.MerchantUser;
import cn.shop.transcation.order.entity.OrderReward;
import cn.shop.transcation.withdraw.entity.Withdraw;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author : 李一涵
 * @date : Created in 2022-4-6 14:19
 * @description :
 */
@Service
public class HomeServiceImpl implements HomeService {

	@Resource
	private FundDetailService fundDetailService;

	@Resource
	private WithdrawService withdrawService;

	@Resource
	private FrontUserService frontUserService;

	@Resource
	private MerchantUserService merchantUserService;
	@Resource
	private OrderRewardService rewardService;


	@Override
	public BigDecimal balance() {
		Map<String, Object> userB = frontUserService.getMap(Wrappers.<FrontUser>query().select("ifnull(sum(balance), 0) balance"));
		Map<String, Object> merchantB = merchantUserService.getMap(Wrappers.<MerchantUser>query().select("ifnull(sum(balance), 0) balance"));
		return NumberUtil.add((BigDecimal) userB.get("balance"), (BigDecimal) merchantB.get("balance"));
	}

	@Override
	public Map<String, Object> orderStatistics(LocalDateTime startTime, LocalDateTime endTime) {
		Map<String, Object> m = rewardService.getMap(Wrappers.<OrderReward>query()
				.apply(startTime != null, "date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null, "date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)
				.select("ifnull(sum(amount), 0) rewards"));
		Map<String, Object> map = fundDetailService.getMap(Wrappers.<FundDetail>query()
				.eq("type", 1)
				.apply(startTime != null, "date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null, "date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)
				.select("ifnull(sum(amount), 0) amounts", "ifnull(sum(platform_share), 0) platfroms", "count(*) count"));
		map.putAll(m);
		return map;
	}

	@Override
	public Map<String, Object> withdrawStatistics(LocalDateTime startTime, LocalDateTime endTime) {
		Map<String, Object> playerBank = withdrawService.getMap(Wrappers.<Withdraw>query()
				.eq("type", 2)
				.eq("state", 3)
				.eq("account_type", 2)
				.apply(startTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)
				.select("ifnull(sum(amount), 0) amounts", "ifnull(sum(actual_amount), 0) actuals", "ifnull(sum(charge_rate), 0) chargeRate", "ifnull(sum(charges), 0) charges", "count(*) count"));
		Map<String, Object> playerAli = withdrawService.getMap(Wrappers.<Withdraw>query()
				.eq("type", 2)
				.eq("state", 3)
				.eq("account_type", 1)
				.apply(startTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)
				.select("ifnull(sum(amount), 0) amounts", "ifnull(sum(actual_amount), 0) actuals", "ifnull(sum(charge_rate), 0) chargeRate", "ifnull(sum(charges), 0) charges", "count(*) count"));
		Map<String, Object> merchantBank = withdrawService.getMap(Wrappers.<Withdraw>query()
				.eq("type", 1)
				.eq("state", 3)
				.eq("account_type", 2)
				.apply(startTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)
				.select("ifnull(sum(amount), 0) amounts", "ifnull(sum(actual_amount), 0) actuals", "ifnull(sum(charge_rate), 0) chargeRate", "ifnull(sum(charges), 0) charges", "count(*) count"));
		Map<String, Object> merchantAli = withdrawService.getMap(Wrappers.<Withdraw>query()
				.eq("type", 1)
				.eq("state", 3)
				.eq("account_type", 1)
				.apply(startTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null, "date_format (payment_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)
				.select("ifnull(sum(amount), 0) amounts", "ifnull(sum(actual_amount), 0) actuals", "ifnull(sum(charge_rate), 0) chargeRate", "ifnull(sum(charges), 0) charges", "count(*) count"));
		return Putif.start()
				.anyway("playBank", playerBank)
				.anyway("playAli", playerAli)
				.anyway("merchantBank", merchantBank)
				.anyway("merchantAli", merchantAli)
				.get();
	}

}
